package com.chezi008.libcontacts.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chezi008.libcontacts.R;
import com.chezi008.libcontacts.bean.ContactBean;
import com.chezi008.libcontacts.listener.ContactListener;
import com.chezi008.libcontacts.listener.ISuspensionInterface;
import com.chezi008.libcontacts.ui.ContactAdapter;
import com.chezi008.libcontacts.utils.LetterComparator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * - @Description:联系人控件
 * - @Author:  chezi008/chezi008@qq.com
 * - @Time:  2019/6/13 9:53
 */
public class ContactView extends FrameLayout {

    private NestedScrollView rvParent;
    private FrameLayout rvTitle;
    private FrameLayout rvBottom;
    private RecyclerView rvList;
    private SuspensionDecoration suspensionDecoration = null;
    private ContactAdapter mAdapter = null;

    public ContactView(@NonNull Context context) {
        this(context, null);
    }

    public ContactView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.view_contact, this);
        initView();
    }

    private void initView() {
        rvParent = findViewById(R.id.rvParent);
        rvList = findViewById(R.id.rvList);
        rvTitle = findViewById(R.id.rvTitle);
        rvBottom = findViewById(R.id.rvBottom);
        WaveSideBarView sideBar = findViewById(R.id.sideBar);

        suspensionDecoration = new SuspensionDecoration(getContext());
        rvList.addItemDecoration(suspensionDecoration);
        rvList.setLayoutManager(new LinearLayoutManager(getContext()));
        sideBar.setOnTouchLetterChangeListener(new WaveSideBarView.OnTouchLetterChangeListener() {
            @Override
            public void onLetterChange(String letter) {
                if(mAdapter == null){
                    return;
                }
                int pos = mAdapter.getLetterPosition(letter);
                if (letter.equals("\u2191")) {
                    pos = -2;
                }
                if (pos >= 0) {
                    int itemHeight = rvList.getMeasuredHeight() / mAdapter.getItemCount();
                    rvParent.scrollTo(0, itemHeight * pos + rvTitle.getMeasuredHeight());
                }else if(pos == -2){
                    rvParent.scrollTo(0, 0);
                }
            }
        });
    }

    public void setHeaderView(View view) {
        rvTitle.removeAllViews();
        rvTitle.addView(view);
        rvParent.scrollTo(0, rvTitle.getMeasuredHeight());
    }

    public void setBottomView(View view) {
        rvBottom.removeAllViews();
        rvBottom.addView(view);
    }

    public void addItemDecoration(RecyclerView.ItemDecoration itemDecoration) {
        rvList.addItemDecoration(itemDecoration);
    }

    public ContactAdapter getmAdapter() {
        return mAdapter;
    }

    public void setmAdapter(ContactAdapter mAdapter) {
        this.mAdapter = mAdapter;
        rvList.setAdapter(mAdapter);
    }

    public void setData(List<? extends ISuspensionInterface> data) {
        suspensionDecoration.setmDatas(data);
    }

    public void setLetterBg(int color){
        suspensionDecoration.setColorTitleBg(color);
    }

    public void setLetterTextColor(int color){
        suspensionDecoration.setColorTitleFont(color);
    }

    public void setLetterTextPaddingStart(int padding){
        suspensionDecoration.setPadding(padding);
    }
}
